package com.example.demo.permission.user.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.common.RespBean;
import com.example.demo.permission.user.pojo.LtUser;
import com.example.demo.permission.user.service.LtUserService;

/**
 * 
 * <p>
 * 功能描述：用户控制层
 * </p>
 * 
 * @author lixh
 * @date 2019年3月6日 上午8:59:04
 * @version 1.0
 */
// @Controller + @ResponseBody 合在一起的作用。
@RestController
@RequestMapping("/system/user")
public class LtUserController {

	@Autowired
	LtUserService userService;

	// 获取单个用户
	@RequestMapping("/id/{userId}")
	public LtUser getUserBuId(@PathVariable Long UserId) {
		return userService.getHrById(UserId);
	}

	// 删除
	@RequestMapping(value = "/{userId}", method = RequestMethod.DELETE)
	public RespBean deleteUser(@PathVariable Long UserId) {
		if (userService.deleteHr(UserId) == 1) {
			return RespBean.ok("删除成功");
		}
		return RespBean.error("删除失败");
	}

	// 更新用户信息
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	public RespBean updateUser(LtUser user) {
		if (userService.updateHr(user) == 1) {
			return RespBean.ok("更新成功");
		}
		return RespBean.error("更新失败");
	}

	// 更新用户角色
	@RequestMapping(value = "/roles", method = RequestMethod.PUT)
	public RespBean updateUserRoles(Long userId, Long[] rids) {
		if (userService.updateHrRoles(userId, rids) == rids.length) {
			return RespBean.ok("更新成功");
		}
		return RespBean.error("更新失败");
	}

	
	// 根据用户名查找用户信息
	@RequestMapping("/{keywords}")
	public List<LtUser> getUserByKeyWords(@PathVariable(required = false) String keywords) {
		List<LtUser> list = new ArrayList<LtUser>();
		list = userService.getHrsByKeywords(keywords);
		return list;
	}

	// 用户注册
	@RequestMapping(value = "/hr/reg", method = RequestMethod.POST)
	public RespBean userReg(String username, String password) {
		int result = userService.hrReg(username, password);
		if (result == 1) {
			return RespBean.ok("注册成功");
		} else if (result == -1) {
			return RespBean.error("用户名重复，注册失败");
		}
		return RespBean.error("注册失败");
	}

}
